﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using NinjaToolbox.SubComponentModule;

namespace NinjaToolbox.BattleSystem
{
    [CustomEditor(typeof(BattleObject))]
    public class BattleObjectInspector : Editor
    {
        public const string SUBCOMPONENT_FILTER_KEYWORD = "BattleObject";

        string[] mCacheFactionConstPopListStrings;
        bool mFactionSettingsToggle;
        bool mCollisionSettingsToggle;
        bool mIsRecordUndo;
        SubComponentContainerGUIDrawer mSubComponentContainerGUIDrawer;


        public override void OnInspectorGUI()
        {
            if (!mIsRecordUndo)
                Undo.RecordObject(target, "BattleObjectInspector");

            serializedObject.Update();

            var factionConstInfoProp = serializedObject.FindProperty("factionConstInfo");
            var factionProp = serializedObject.FindProperty("faction");
            var factionComparer = serializedObject.FindProperty("factionComparer");

            var collisionSettings_PhysicsObject = serializedObject.FindProperty("collisionSettings.physicsObject");
            var collisionSettings_enableTriggerStayEvent = serializedObject.FindProperty("collisionSettings.enableTriggerStayEvent");
            var collisionSettings_enableTriggerExitEvent = serializedObject.FindProperty("collisionSettings.enableTriggerExitEvent");
            var collisionSettings_enableSelfTriggerEvent = serializedObject.FindProperty("collisionSettings.enableSelfTriggerEvent");
            var attachCollidersProp = serializedObject.FindProperty("attachColliders");

            if (mSubComponentContainerGUIDrawer == null)
            {
                var subComponentContainer = (base.target as BattleObject).BattleSubComponentContainer;
                mSubComponentContainerGUIDrawer = new SubComponentContainerGUIDrawer(subComponentContainer, "BattleSubComponents");

                mSubComponentContainerGUIDrawer.ChangeFilterKeyword(SUBCOMPONENT_FILTER_KEYWORD);
            }

            EditorGUILayout.Space();

            using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
            {
                mFactionSettingsToggle = EditorGUILayout.Foldout(mFactionSettingsToggle, "Faction Settings");

                if (mFactionSettingsToggle)
                {
                    EditorGUI.indentLevel++;

                    EditorGUILayout.PropertyField(factionConstInfoProp);

                    var factionConstObjectReferenceValue = factionConstInfoProp.objectReferenceValue;

                    if (factionConstObjectReferenceValue != null)
                    {
                        var concertFactionConstInfo = factionConstObjectReferenceValue as BattleObject_FactionConstInfo;

                        if (mCacheFactionConstPopListStrings == null || mCacheFactionConstPopListStrings.Length != concertFactionConstInfo.constInfoArray.Length)
                        {
                            mCacheFactionConstPopListStrings = concertFactionConstInfo.constInfoArray.Select(m => m.factionName).ToArray();
                        }

                        var index = Array.FindIndex(concertFactionConstInfo.constInfoArray, m => m.id == factionProp.intValue);
                        index = Mathf.Max(index, 0);

                        index = EditorGUILayout.Popup(new GUIContent(factionProp.displayName), index, mCacheFactionConstPopListStrings);

                        factionProp.intValue = concertFactionConstInfo.constInfoArray[index].id;

                        EditorGUILayout.PropertyField(factionComparer);
                    }
                    else
                    {
                        EditorGUILayout.PropertyField(factionProp);
                    }

                    EditorGUI.indentLevel--;
                }

                mCollisionSettingsToggle = EditorGUILayout.Foldout(mCollisionSettingsToggle, "Collision Settings");

                if (mCollisionSettingsToggle)
                {
                    EditorGUI.indentLevel++;

                    EditorGUILayout.PropertyField(collisionSettings_PhysicsObject);
                    EditorGUILayout.PropertyField(collisionSettings_enableSelfTriggerEvent);
                    EditorGUILayout.PropertyField(collisionSettings_enableTriggerStayEvent);
                    EditorGUILayout.PropertyField(collisionSettings_enableTriggerExitEvent);

                    EditorGUILayout.PropertyField(attachCollidersProp, true);

                    EditorGUI.indentLevel--;
                }

                EditorGUILayout.Space();

                mSubComponentContainerGUIDrawer.DrawGUI();

                EditorGUILayout.Space();

                if (changeCheckScope.changed)
                {
                    GUI.changed = true;

                    if (!Application.isPlaying)
                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEngine.SceneManagement.SceneManager.GetActiveScene());

                    mIsRecordUndo = false;

                    serializedObject.ApplyModifiedProperties();
                }
            }
        }
    }
}
